package xcmg.device.web.warehouse;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.warehouse.WarehouseDeliveryAddVO;
import xcmg.device.service.vo.warehouse.WarehouseDeliveryVO;
import xcmg.device.service.warehouse.WarehouseDeliveryService;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;

@RestController
@RequestMapping("/warehouseDelivery")
public class WarehouseDeliveryController extends BasicController{
    @Autowired
    private WarehouseDeliveryService warehouseDeliveryService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "增加出库记录")
    @FastMappingInfo(needLogin = true)
    public ActionResult warehouse(@RequestBody WarehouseDeliveryAddVO warehouseDeliveryAddVO)throws Exception{
        if(null == warehouseDeliveryAddVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode = warehouseDeliveryService.add(warehouseDeliveryAddVO, getUserId(), getOrgId());
        return actionResult(errorCode);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "更新出库记录")
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody WarehouseDeliveryVO warehouseDeliveryVO)throws Exception{
        if(null == warehouseDeliveryVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode = warehouseDeliveryService.update(warehouseDeliveryVO);
        return actionResult(errorCode);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ApiOperation("出库记录删除")
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids)throws Exception{
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(warehouseDeliveryService.delete(Arrays.asList(ids), getOrgId()));
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @ApiOperation(value = "查找单条记录详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult warehouse(String id)throws Exception{
        return actionValueResult(warehouseDeliveryService.findByPK(id));
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation("查询出库记录列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<WarehouseDeliveryVO> condition)throws Exception {
        WarehouseDeliveryVO warehouseDeliveryVO =condition.getFilters();
        if(warehouseDeliveryVO == null){
            warehouseDeliveryVO = new WarehouseDeliveryVO();
        }
        warehouseDeliveryVO.setOrgId(getOrgId());
        condition.setFilters(warehouseDeliveryVO);
        PageCommonVO pageCommonVO= warehouseDeliveryService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @ApiOperation("导出出库数据列表")
    @FastMappingInfo(needLogin = true)
    public void export(@RequestBody String[] ids)throws Exception {
        warehouseDeliveryService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.GET)
    @ApiOperation("导出全部出库数据列表")
    @FastMappingInfo(needLogin = true)
    public void exportAll(WarehouseDeliveryVO condition)throws Exception {
        warehouseDeliveryService.exportAll(condition);
    }
}
